package com.rrx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;

@Controller
public class FileController {

    @RequestMapping("/fileUpload")
    @ResponseBody
    public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) {
        System.out.println("上传图片!");
        System.out.println(request.getParameter("idcard"));
        long  startTime=System.currentTimeMillis();
        String path = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/")+request.getContextPath()+file.getOriginalFilename();
        File newFile=new File(path);
        try {
            file.transferTo(newFile);
            long  endTime=System.currentTimeMillis();
            return "success."+String.valueOf(endTime-startTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }
}
